package com.alexwan.nightnote.ui.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.alexwan.nightnote.R;
import com.alexwan.nightnote.widget.DragLayout;

/**
 * App首页 Frgment
 * 
 * @author alexwan
 * 
 */
public class HomeActivity extends FragmentActivity implements
		OnItemClickListener {
	private DragLayout mDragLayout;
	private ListView mListView;

	public static void launch(Context context) {
		Intent intent = new Intent(context, HomeActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_ui);
		// 初始化侧边栏
		initDraglayout();
		initView();
		// 初始化Fragment
		initFragment();
	}

	private FragmentManager mFragmentManager;
	private Fragment[] mFragments;
	private FragmentTransaction mFragmentTransaction;

	private void initFragment() {
		mFragments = new Fragment[3];
		mFragmentManager = getSupportFragmentManager();
		mFragments[0] = mFragmentManager.findFragmentById(R.id.fragment_main);
		mFragments[1] = mFragmentManager
				.findFragmentById(R.id.fragement_search);
		mFragments[2] = mFragmentManager
				.findFragmentById(R.id.fragement_setting);
		mFragmentTransaction = mFragmentManager.beginTransaction()
				.hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2]);
		mFragmentTransaction.show(mFragments[0]).commit();
		setFragmentIndicator();
	}

	private RadioGroup mRadioGroup;
	private RadioButton mOneButton, mTwoButton, mThreeButton;

	private void setFragmentIndicator() {
		mRadioGroup = (RadioGroup) findViewById(R.id.bottom_bar_radio_group);
		mOneButton = (RadioButton) findViewById(R.id.radio_btn_one);
		mTwoButton = (RadioButton) findViewById(R.id.radio_btn_two);
		mThreeButton = (RadioButton) findViewById(R.id.radio_btn_three);
		mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				mFragmentTransaction = mFragmentManager.beginTransaction()
						.hide(mFragments[0]).hide(mFragments[1])
						.hide(mFragments[2]);
				switch (checkedId) {
				case R.id.radio_btn_one:
					mFragmentTransaction.show(mFragments[0]).commit();
					break;
				case R.id.radio_btn_two:
					mFragmentTransaction.show(mFragments[1]).commit();
					break;
				case R.id.radio_btn_three:
					mFragmentTransaction.show(mFragments[2]).commit();
					break;

				default:
					break;
				}
			}
		});
	}

	private void initView() {
		mListView = (ListView) findViewById(R.id.home_ui_list);
		mListView.setOnItemClickListener(this);

	}

	/**
	 * 初始化侧边栏
	 */
	private void initDraglayout() {
		mDragLayout = (DragLayout) findViewById(R.id.drag_layout);
		mDragLayout.setDragListener(new DragLayout.DragListener() {

			@Override
			public void onOpen() {
				// 侧边栏关闭
			}

			@Override
			public void onDrag(float percent) {
				// 侧边栏拖动

			}

			@Override
			public void onClose() {
				// ViewHelper.setAlpha(iv_icon, 1 - percent);
			}
		});
	}

	private static boolean isExit = false;
	private static int DELAY_TIME = 2000;
	private static int EXIT_CODE = 0;

	@Override
	public void onBackPressed() {
		exit();
	}

	private void exit() {
		Timer mExitTimer = null;
		if (!isExit) {
			isExit = true;
			Toast.makeText(this, "再次点击退出程序", Toast.LENGTH_SHORT).show();
			mExitTimer = new Timer();
			mExitTimer.schedule(new TimerTask() {

				@Override
				public void run() {
					isExit = false;
				}
			}, DELAY_TIME);
		} else {
			finish();
			System.exit(EXIT_CODE);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

	}

}
